#include"headers.h"
#include"system.h"

System::System(int width, int height, int bpp, bool full)
{
    screen_height = height;
    screen_width = width;
    screen_res = bpp;
    fullscreen = full;
    quit = false;
}

void System::Load_Config(string location)
{
    ifstream file;

    file.open( location.c_str() );

    string temp_load;
    cout<<"Engine Ver: "<< VERSION <<"\n";
    while(file>>temp_load)
    {
        if(temp_load == "Version")
        {
            file>>version;
            cout<<"File Version: "<< version <<"\n";
            if(version != VERSION)
            {
                cout<<"\aWARNING DIFFERENT File Version! ~"<< version<<"\n";
            }
            if(version > VERSION)
            {
                cout<<"\aFile Version is too advanced/ or build doesn't exist!\n";
            }
            if(version < VERSION)
            {
                cout<<"\aFile Version is  Outdated! will continue to use..\n";
                cout<<"but not all features  will be the same :)\n";
            }
        }

        else if(temp_load == "Title")
        {
            file>>window_title;
            cout<<"Setting Window title to "<< window_title<<"\n";
        }
        else if(temp_load == "SCREEN_W")
        {
            file>>screen_width;
            cout<<"Screen Width: "<< screen_width<<"\n";
        }

        else if(temp_load == "Language")
        {
            file>>language;
            cout<<"Language is set to: "<< language <<"\n";
        }

        else if(temp_load == "SCREEN_H")
        {
            file>>screen_height;
            cout<<"Screen Height: "<< screen_height<<"\n";
        }

        else if(temp_load == "Fullscreen")
        {
            file>>temp_load;
            if(temp_load == "False")
            {
                fullscreen = false;
                cout<<"fullscreen false\n";
            }
            else
            {
                fullscreen = true;
                cout<<"fullscreen true\n";
            }
        }

        else if(temp_load == "Dificulty")
        {
            file>>dificulty;
            while(dificulty > 10)
            {
                dificulty /= 2;
            }
            cout<<"Max dificulty set to: " << dificulty << "\n";
        }

        else if(temp_load == "Enemies")
        {
            file>>enemies;
            cout<<"Enemies loaded;count: " << enemies << "\n";
        }
    }
    file.close();
}

void System::Init(bool defaults)
{
    //seed for time
    time_t secs;
    time(&secs);
    srand((unsigned int)secs);

    //set up for allegro
    allegro_init();
    install_keyboard();
    install_timer();
    install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, 0);
    install_mouse();

    poll_mouse();

    //function that does NOT Load defaults, but user settings
    if(!defaults)
    {
        /*if ((cpu_capabilities & (CPU_FPU | CPU_MMX)) == (CPU_FPU | CPU_MMX))
        {
            cout<<"CPU has both an FPU and MMX instructions!\n";
        }*/
        cout<<"====Allegro Version 4.2.2===============================\n\n";
        Load_Config("system.ini");
        set_window_title(window_title.c_str());
    }
    set_color_depth(screen_res);
    if(fullscreen)
    {
        if(set_gfx_mode(GFX_AUTODETECT,screen_width,screen_height,0,0) != 0)
        {
            cout<<"Unable to set in Fullscreen Mode. Using Windowed mode to set Values";
            if(set_gfx_mode(GFX_AUTODETECT_WINDOWED,screen_width,screen_height,0,0) != 0)
            {
                cout<<"Error! The Program could not set Graphical resolution\n";
                cout<<"Please Contact the project leader of this program for help or on info:\n";
            }
        }
    }
    else
    {
        set_gfx_mode(GFX_AUTODETECT_WINDOWED,screen_width,screen_height,0,0);
    }

}
